const app = getApp();
import apiResult from '../../../utils/api-result';
import apiService from '../../../utils/api-service'; 
import {Config, getPhone,getInfo} from '../../../config/api.js';
Page({
  data: {
    logo: "",
    statistics: {},
    isLogin: false,
    username: "",
    password: "",
    loginMessage: false,
    noticeCount: 0,
    info:{},
    showAuth: true,
    userInfo:{}
  },
  onLoad: function () { 
    var that = this;
    that.setData({
      logo: app.globalData.logo
    })
   
  },
  async onShow() {
    var that = this;
    wx.getSetting({
      success: (res => {
        console.log(res, 'getSetting')
        if (res.authSetting['scope.userInfo']) {
          // that.setData({
          //   isLogin: true
          // })
        }
      })
    })
    ///
    that.setData({
      loadModal:true,
      loginMessage: false
    })
    var token = wx.getStorageSync(Config.Token)
    console.log(token)
    if(token){
      const statistics = await this.adminGetStatistics();
      const noticeCount = await this.getNoticeCount();
      that.setData({
        user: statistics.user,
        statistics: statistics,
        noticeCount: noticeCount,
        isLogin: true
      });
    }
    that.setData({
      loadModal:false
    })
  },
  /**
   * 输入账号
   */
  usernameInput(e){
    this.setData({
      username: e.detail.value
    });
  },
  /**
   * 输入密码
   * @param {*} e 
   */
  passwordInput(e){
    this.setData({
      password: e.detail.value
    });
  },
  /**
   * 登录
   */
  userInfoHandler(e){
    if (e.detail.userInfo) {
      // that.setData({
      //   showAuth: false
      // })
      this.getInfo()
    } else {
      wx.showModal({
        content: "请确认授权"
      })
    }

  },
  getOpenId(appid, code, wxUser){
    let that = this
    wx.request({
      url: getInfo(appid),
      // url: "https://web.dingn.cn/api/mini/" + appid + '/info',
      method:'post',
      data:{
        code: code,
        wxUser: wxUser
      },
      async success(res){
        console.log(res, 'getOpenId')
        if(res.data.data.phone){
          wx.setStorageSync(Config.Token, res.data.data.accessToken)
          const statistics = await that.adminGetStatistics();
          apiResult.success("登录成功");
          that.setData({
            user: statistics.user,
            statistics: statistics
          });
          that.setData({
            isLogin: true
          })
        }
        that.setData({
          userInfo:res.data.data,
          showAuth: false
        })
      }
    })
  },
  onGetPhoneNumber(e){
    console.log(e,'sj')
    let appid = wx.getAccountInfoSync().miniProgram.appId;
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {

    } else{
      this.getPhone(appid,e.detail)
    }
  },
   getPhone(appid, val){
    let user = this.data.userInfo
    let info = this.data.info
    let that = this
    console.log(this.data.userInfo)
    return wx.request({
      url:getPhone(appid),
      // url: "https://web.dingn.cn/api/mini/" + appid + '/phone',
      method:'post',
      data: {
        "session_key": user.session_key,
        "encryptedData": val.encryptedData,
        "iv": val.iv,
        "appType": 0,
        "openid": user.openid,
        "unionid": user.unionid,
        "userInfo": info.userInfo
      },
      async success(res) {
        console.log(res)
        wx.setStorageSync(Config.Token, res.data.data.accessToken)
        const statistics = await that.adminGetStatistics();
        apiResult.success("登录成功");
        that.setData({
          user: statistics.user,
          statistics: statistics
        });
        that.setData({
          isLogin: true
        })
      }
    })
  },
  getInfo(){
    let appid = wx.getAccountInfoSync().miniProgram.appId;
    let code = ''
    let that = this
    wx.login({
      success: res => {
        code = res.code
        console.log(res, appid, 'login')
        wx.getUserInfo({
          success: (info) => {
            // this.info = info
            that.setData({
              info: info
            });
            console.log(info, 'getUserInfo')
            that.getOpenId(appid, code, info)
           
          }
        })
      }
    })
  },
  
  async login(e){
    var that = this;
    if(!this.data.username){
      apiResult.warn("账号不能为空");
      return ;
    }
    if(!this.data.password){
      apiResult.warn("密码不能为空");
      return ;
    }
    const param = {
      username: this.data.username,
      password: this.data.password
    }
    try {
      const result = await apiService.adminLogin(param);
      wx.setStorageSync(Config.Token, result.access_token)
      const statistics = await this.adminGetStatistics();
      apiResult.success("登录成功");
      that.setData({
        user: statistics.user,
        statistics: statistics
      });
      that.setData({
        isLogin: true
      })
    } catch (error) {
      wx.setStorageSync(Config.Token, "")
      apiResult.error("密码错误");
    }
  },
  /**
   * 复制
   * @param {*} e 
   */
  CopyLink(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '已复制',
          duration: 1000,
        })
      }
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },

  /**
   * 跳转到日记页面
   */
  toJournalPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/journal/home/index"
    })
  },
  /**
   * 跳转到捕捉页面(光影)
   */
  toPhotosPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/photos/home/index"
    })
  },
  /**
   * 跳转到友链管理页面
   */
  toLinksPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/links/home/index"
    })
  },
  /**
   * 跳转到服务器信息页面
   */
  toEnvironmentPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/environments/index"
    })
  },
  /**
   * 跳转到个人信息
   */
  toUserPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/user/index"
    })
  },

  /**
   * 跳转到评论页
   */
  toCommentPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/comment/index"
    })
  },
  toCategoryPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/category/index"
    })
  },
  toTagPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/tag/index"
    })
  },
  toArticlePage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/article/index"
    })
  },
  toAddArticlePage() {
    this.setData({
      modalName: null
    })
    // apiResult.warn("请前往PC端");
    wx.navigateTo({
      url:"/pages/admin/add/index"
    })
  },
  toMenuPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/menu/list/index"
    })
  },
  toThemePage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/theme/list/index"
    })
  },
  toAttachmentPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/attachment/home/index"
    })
  },
  toSettingPage() {
    this.setData({
      modalName: null
    })
    wx.navigateTo({
      url:"/pages/admin/setting/index"
    })
  },
  
  /**
   * 获取站点及博主信息
   */
  async adminGetStatistics(){
    try {
      const result = await apiService.adminGetStatistics();
      return result;
    } catch (error) {
      return error.message;
    }
  },

  getMessage() {
    var that = this;
    const loginMessage = that.data.loginMessage;
    if(loginMessage){
      that.setData({
        loginMessage: false
      })
    }else{
      that.setData({
        loginMessage: true
      })
    }
  },
  /**
   * 获取消息数量
   */
  async getNoticeCount(){
    const param = {
      page: 1,
      size: 0,
      status: "AUDITING"
    }
    const postResult = await apiService.adminGetPostComment(param);
    const sheetResult = await apiService.adminGetSheetComment(param);
    return postResult.total+sheetResult.total;
  }
})